package com.hsurosy.hsuchain.nft.order.domain.entity;

import com.hsurosy.hsuchain.nft.api.goods.constant.GoodsType;
import com.hsurosy.hsuchain.nft.api.order.constant.TradeOrderEvent;
import com.hsurosy.hsuchain.nft.api.order.constant.TradeOrderState;
import com.hsurosy.hsuchain.nft.api.pay.constant.PayChannel;
import com.hsurosy.hsuchain.nft.api.user.constant.UserType;
import com.hsurosy.hsuchain.nft.datasource.domain.entity.BaseEntity;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 交易订单流水实体类
 *
 * @Author Hsu琛君珩
 * @Date 2024-10-05 17:07
 * @Description
 * @Version: v1.0.0
 */
@Data
@NoArgsConstructor
public class TradeOrderStream extends BaseEntity {

    /**
     * 订单ID
     */
    private String orderId;

    /**
     * 买家ID
     */
    private String buyerId;

    /**
     * 买家ID类型
     */
    private UserType buyerType;

    /**
     * 卖家ID
     */
    private String sellerId;

    /**
     * 卖家ID类型
     */
    private UserType sellerType;

    /**
     * 订单幂等号，用于幂等操作
     */
    private String identifier;

    /**
     * 订单金额
     */
    private BigDecimal orderAmount;

    /**
     * 商品数量
     */
    private int itemCount;

    /**
     * 商品单价
     */
    private BigDecimal itemPrice;

    /**
     * 已支付金额
     */
    private BigDecimal paidAmount;

    /**
     * 支付成功时间
     */
    private Date paySucceedTime;

    /**
     * 订单确认时间
     */
    private Date orderConfirmedTime;

    /**
     * 订单完成时间
     */
    private Date orderFinishedTime;

    /**
     * 订单关闭时间
     */
    private Date orderClosedTime;

    /**
     * 商品ID
     */
    private String goodsId;

    /**
     * 商品类型
     */
    private GoodsType goodsType;

    /**
     * 商品图片URL
     */
    private String goodsPicUrl;

    /**
     * 商品名称
     */
    private String goodsName;

    /**
     * 支付方式
     */
    private PayChannel payChannel;

    /**
     * 支付流水号
     */
    private String payStreamId;

    /**
     * 订单状态
     */
    private TradeOrderState orderState;

    /**
     * 关单类型，例如超时关闭等
     */
    private String closeType;

    /**
     * 流水类型，用于标识订单的操作事件类型
     */
    private TradeOrderEvent streamType;

    /**
     * 流水幂等号，用于幂等处理
     */
    private String streamIdentifier;

    /**
     * 通过传入的交易订单和操作类型构造订单流水
     *
     * @param tradeOrder 原始交易订单对象
     * @param streamType 流水类型，表示订单的操作事件
     * @param streamIdentifier 流水幂等号，用于幂等操作
     */
    public TradeOrderStream(TradeOrder tradeOrder, TradeOrderEvent streamType, String streamIdentifier) {
        // 设置订单的基本信息
        this.orderId = tradeOrder.getOrderId();
        this.buyerId = tradeOrder.getBuyerId();
        this.buyerType = tradeOrder.getBuyerType();
        this.sellerId = tradeOrder.getSellerId();
        this.sellerType = tradeOrder.getSellerType();
        this.identifier = tradeOrder.getIdentifier();
        this.orderAmount = tradeOrder.getOrderAmount();
        this.paidAmount = tradeOrder.getPaidAmount();
        this.paySucceedTime = tradeOrder.getPaySucceedTime();
        this.orderConfirmedTime = tradeOrder.getOrderConfirmedTime();
        this.orderFinishedTime = tradeOrder.getOrderFinishedTime();
        this.orderClosedTime = tradeOrder.getOrderClosedTime();
        this.goodsId = tradeOrder.getGoodsId();
        this.goodsType = tradeOrder.getGoodsType();
        this.goodsName = tradeOrder.getGoodsName();
        this.goodsPicUrl = tradeOrder.getGoodsPicUrl();
        this.payChannel = tradeOrder.getPayChannel();
        this.payStreamId = tradeOrder.getPayStreamId();
        this.orderState = tradeOrder.getOrderState();
        this.closeType = tradeOrder.getCloseType();
        this.itemCount = tradeOrder.getItemCount();
        this.itemPrice = tradeOrder.getItemPrice();

        // 设置订单流水的操作类型及幂等号
        this.streamType = streamType;
        this.streamIdentifier = streamIdentifier;

        // 继承订单的乐观锁版本和删除标记
        super.setLockVersion(tradeOrder.getLockVersion());
        super.setDeleted(tradeOrder.getDeleted());
    }
}
